package cn.edu.hust.sse.software7.class3.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.hust.sse.software7.class3.common.DateTools;
import cn.edu.hust.sse.software7.class3.common.MD5Tool;
import cn.edu.hust.sse.software7.class3.entities.EBProduct;
import cn.edu.hust.sse.software7.class3.entities.EBShoppingCar;
import cn.edu.hust.sse.software7.class3.entities.EBUser;
import cn.edu.hust.sse.software7.class3.service.EBProductService;
import cn.edu.hust.sse.software7.class3.service.EBShoppingCarService;
import cn.edu.hust.sse.software7.class3.service.EBUserService;
import cn.edu.hust.sse.software7.class3.serviceImpl.EBProductServiceImpl;
import cn.edu.hust.sse.software7.class3.serviceImpl.EBShoppingCarServiceImpl;
import cn.edu.hust.sse.software7.class3.serviceImpl.EBUserServiceImpl;

public class UserServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2027900510195050330L;
	EBUserService us = new EBUserServiceImpl();
	EBShoppingCarService ebscs = new EBShoppingCarServiceImpl();
	EBProductService ebps = new EBProductServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String method = req.getParameter("method");
		if (method.equals("logout")) {
			HttpSession session = req.getSession();
			session.removeAttribute("userId");
			resp.sendRedirect("eBusiness/top.jsp");
		}
		if (method.equals("addShopCar")) {
			HttpSession session = req.getSession();
			Long userId = (Long) session.getAttribute("userId");
			String prodid = req.getParameter("pid2SC");
			Long pid = new Long(prodid);
			EBShoppingCar car = new EBShoppingCar(userId, pid);
			boolean check = ebscs.isEsistCar(car);
			if(!check){
			ebscs.createSCar(car);
			}
			resp.sendRedirect("eBusiness/prod/watchProds.jsp");
		}

		if (method.equals("shopcar")) {
			HttpSession session = req.getSession();
			String user = req.getParameter("userId");
			Long userId = new Long(user);
			List<EBShoppingCar> car = ebscs.getByUserId(userId);
			if (null != car && !car.isEmpty()) {
				List<Long> prodIds = new ArrayList<Long>();
				for (EBShoppingCar c : car) {
					Long pid = c.getProductId();
					if (!prodIds.contains(pid)) {
						prodIds.add(pid);
					}
				}
				List<EBProduct> prods = ebps.getByProdsId(prodIds);
				session.setAttribute("prodInSCList", prods);
			}else{
				session.removeAttribute("prodInSCList");
			}
			resp.sendRedirect("eBusiness/prod/watchShopCar.jsp");
		}
		if (method.equals("deleteWatch")) {
			HttpSession session = req.getSession();
			String id = req.getParameter("id");
			Long userId = (Long) session.getAttribute("userId");
			Long pid = new Long(id);
			ebscs.deleteSCar(new EBShoppingCar(userId, pid));
			List<EBShoppingCar> car = ebscs.getByUserId(userId);
			if (null != car && !car.isEmpty()) {
				List<Long> prodIds = new ArrayList<Long>();
				for (EBShoppingCar c : car) {
					Long prodid = c.getProductId();
					if (!prodIds.contains(prodid)) {
						prodIds.add(prodid);
					}
				}
				List<EBProduct> prods = ebps.getByProdsId(prodIds);
				session.setAttribute("prodInSCList", prods);
			}
			else{
				session.removeAttribute("prodInSCList");
			}
			resp.sendRedirect("eBusiness/prod/watchShopCar.jsp");
		}
		if (method.equals("watchUser")) {
			HttpSession session = req.getSession();
			Long userId = (Long) session.getAttribute("userId");
			EBUser user = us.getUserById(userId);
			session.setAttribute("user", user);
			resp.sendRedirect("eBusiness/user/updateUser.jsp");
		}

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String method = req.getParameter("method");
		if (method.equals("login")) {
			HttpSession session = req.getSession();
			String userName = req.getParameter("un");
			String passwd = req.getParameter("pw");
			EBUser user = new EBUser(userName, MD5Tool.Encrypt(passwd));
			EBUser u = us.validateUser(user);
			if (null != u) {
				session.setAttribute("userId", u.getUserId());
				resp.sendRedirect("eBusiness/top.jsp");
			} else {
				session.setAttribute("message", "用户名或密码错误");
				resp.sendRedirect("eBusiness/top.jsp");
			}
		}
		if (method.equals("regist")) {
			HttpSession session = req.getSession();
			String userName = req.getParameter("userName");
			String passwd = req.getParameter("passwd");
			String repasswd = req.getParameter("repasswd");
			String birth = req.getParameter("birth");
			String cellphone = req.getParameter("cellphone");
			String email = req.getParameter("email");
			if (passwd.equals(repasswd)) {
				EBUser user = new EBUser(userName, MD5Tool.Encrypt(passwd),
						email, cellphone);
				user.setBirth(DateTools.getDate(birth));
				if (us.isExistUser(user)) {
					session.setAttribute("message", "该用户已经存在");
					resp.sendRedirect("eBusiness/user/regist.jsp");
				} else {
					us.addUser(user);
					session.setAttribute("message", "注册成功");
					session.setAttribute("userId", user.getUserId());
					resp.sendRedirect("eBusiness/user/regist.jsp");
				}
			} else {
				session.setAttribute("message", "密码不一致");
				resp.sendRedirect("eBusiness/user/regist.jsp");
			}

		}
		if (method.equals("updateUser")) {
			HttpSession session = req.getSession();
			String userName = req.getParameter("userName");
			String passwd = req.getParameter("passwd");
			String newpasswd = req.getParameter("newpasswd");
			String repasswd = req.getParameter("repasswd");
			String birth = req.getParameter("birth");
			String cellphone = req.getParameter("cellphone");
			String email = req.getParameter("email");
			if (newpasswd.equals(repasswd)) {
				Long userId = (Long) req.getAttribute("userId");
				EBUser user = us.getUserById(userId);
				if (MD5Tool.Encrypt(passwd).equals(user.getPassword())) {
					user.setPassword(MD5Tool.Encrypt(newpasswd));
					if (null != userName) {
						user.setUserName(userName);
					}
					if (null != cellphone) {
						user.setCellPhone(cellphone);
					}
					if (null != email) {
						user.setEmail(email);
					}
					if (null != birth) {
						user.setBirth(DateTools.getDate(birth));
					}
					us.updateUser(user);
					session.setAttribute("user", user);
					session.setAttribute("message", "您的信息已经成功修改");
					resp.sendRedirect("eBusiness/user/updateUser.jsp");
				} else {
					session.setAttribute("message", "密码错误");
					resp.sendRedirect("eBusiness/user/updateUser.jsp");
				}
			} else {
				session.setAttribute("message", "再次输入新密码不一致");
				resp.sendRedirect("eBusiness/user/updateUser.jsp");
			}
		}
	}
}
